package barcan.flutter.io.image_picker.statusbar;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.ColorInt;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/*
 * created by wdlmm1214
 * on 2020/1/9
 */public class StatusBarHelper {
    private static final int STATUSBAR_TYPE_DEFAULT = 0;
    private static final int STATUSBAR_TYPE_MIUI = 1;
    private static final int STATUSBAR_TYPE_FLYME = 2;
    private static final int STATUSBAR_TYPE_ANDROID6 = 3;
    private static final int STATUS_BAR_DEFAULT_HEIGHT_DP = 25;
    public static float sVirtualDensity = -1.0F;
    public static float sVirtualDensityDpi = -1.0F;
    private static int sStatusbarHeight = -1;
    private static int mStatuBarType = 0;
    private static Integer sTransparentValue;

    public StatusBarHelper() {
    }

    public static void translucent(Activity activity) {
        translucent(activity, 1073741824);
    }

    private static boolean supportTranslucent() {
        return Build.VERSION.SDK_INT >= 19 && !QMUIDeviceHelper.isEssentialPhone();
    }

    @TargetApi(19)
    public static void translucent(Activity activity, @ColorInt int colorOn5x) {
        if (supportTranslucent()) {
            Window window = activity.getWindow();
            if (!QMUIDeviceHelper.isMeizu() && !QMUIDeviceHelper.isMIUI()) {
                if (QMUINotchHelper.isNotchOfficialSupport()) {
                    WindowManager.LayoutParams params = window.getAttributes();
                    params.layoutInDisplayCutoutMode = 1;
                    window.setAttributes(params);
                }

                if (Build.VERSION.SDK_INT >= 21) {
                    window.getDecorView().setSystemUiVisibility(1280);
                    if (Build.VERSION.SDK_INT >= 23 && supportTransclentStatusBar6()) {
                        window.clearFlags(67108864);
                        window.addFlags(-2147483648);
                        window.setStatusBarColor(0);
                    } else {
                        window.clearFlags(67108864);
                        window.addFlags(-2147483648);
                        window.setStatusBarColor(colorOn5x);
                    }
                }

            } else {
                window.setFlags(67108864, 67108864);
            }
        }
    }

    public static boolean setStatusBarLightMode(Activity activity) {
        if (activity == null) {
            return false;
        } else if (QMUIDeviceHelper.isZTKC2016()) {
            return false;
        } else if (mStatuBarType != 0) {
            return setStatusBarLightMode(activity, mStatuBarType);
        } else {
            if (Build.VERSION.SDK_INT >= 19) {
                if (isMIUICustomStatusBarLightModeImpl() && MIUISetStatusBarLightMode(activity.getWindow(), true)) {
                    mStatuBarType = 1;
                    return true;
                }

                if (FlymeSetStatusBarLightMode(activity.getWindow(), true)) {
                    mStatuBarType = 2;
                    return true;
                }

                if (Build.VERSION.SDK_INT >= 23) {
                    Android6SetStatusBarLightMode(activity.getWindow(), true);
                    mStatuBarType = 3;
                    return true;
                }
            }

            return false;
        }
    }

    private static boolean setStatusBarLightMode(Activity activity, int type) {
        if (type == 1) {
            return MIUISetStatusBarLightMode(activity.getWindow(), true);
        } else if (type == 2) {
            return FlymeSetStatusBarLightMode(activity.getWindow(), true);
        } else {
            return type == 3 ? Android6SetStatusBarLightMode(activity.getWindow(), true) : false;
        }
    }

    public static boolean setStatusBarDarkMode(Activity activity) {
        if (activity == null) {
            return false;
        } else if (mStatuBarType == 0) {
            return true;
        } else if (mStatuBarType == 1) {
            return MIUISetStatusBarLightMode(activity.getWindow(), false);
        } else if (mStatuBarType == 2) {
            return FlymeSetStatusBarLightMode(activity.getWindow(), false);
        } else {
            return mStatuBarType == 3 ? Android6SetStatusBarLightMode(activity.getWindow(), false) : true;
        }
    }

    @TargetApi(23)
    private static int changeStatusBarModeRetainFlag(Window window, int out) {
        out = retainSystemUiFlag(window, out, 1024);
        out = retainSystemUiFlag(window, out, 4);
        out = retainSystemUiFlag(window, out, 2);
        out = retainSystemUiFlag(window, out, 4096);
        out = retainSystemUiFlag(window, out, 1024);
        out = retainSystemUiFlag(window, out, 512);
        return out;
    }

    public static int retainSystemUiFlag(Window window, int out, int type) {
        int now = window.getDecorView().getSystemUiVisibility();
        if ((now & type) == type) {
            out |= type;
        }

        return out;
    }

    @TargetApi(23)
    public static boolean Android6SetStatusBarLightMode(Window window, boolean light) {
        View decorView = window.getDecorView();
        int systemUi = light ? 8192 : 256;
        systemUi = changeStatusBarModeRetainFlag(window, systemUi);
        decorView.setSystemUiVisibility(systemUi);
        if (QMUIDeviceHelper.isMIUIV9()) {
            MIUISetStatusBarLightMode(window, light);
        }

        return true;
    }

    public static boolean MIUISetStatusBarLightMode(Window window, boolean light) {
        boolean result = false;
        if (window != null) {
            Class clazz = window.getClass();

            try {
                Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                int darkModeFlag = field.getInt(layoutParams);
                Method extraFlagField = clazz.getMethod("setExtraFlags", Integer.TYPE, Integer.TYPE);
                if (light) {
                    extraFlagField.invoke(window, darkModeFlag, darkModeFlag);
                } else {
                    extraFlagField.invoke(window, 0, darkModeFlag);
                }

                result = true;
            } catch (Exception var8) {
            }
        }

        return result;
    }

    private static boolean isMIUICustomStatusBarLightModeImpl() {
        if (QMUIDeviceHelper.isMIUIV9() && Build.VERSION.SDK_INT < 23) {
            return true;
        } else {
            return QMUIDeviceHelper.isMIUIV5() || QMUIDeviceHelper.isMIUIV6() || QMUIDeviceHelper.isMIUIV7() || QMUIDeviceHelper.isMIUIV8();
        }
    }

    public static boolean FlymeSetStatusBarLightMode(Window window, boolean light) {
        boolean result = false;
        if (window != null) {
            Android6SetStatusBarLightMode(window, light);

            try {
                WindowManager.LayoutParams lp = window.getAttributes();
                Field darkFlag = WindowManager.LayoutParams.class.getDeclaredField("MEIZU_FLAG_DARK_STATUS_BAR_ICON");
                Field meizuFlags = WindowManager.LayoutParams.class.getDeclaredField("meizuFlags");
                darkFlag.setAccessible(true);
                meizuFlags.setAccessible(true);
                int bit = darkFlag.getInt((Object)null);
                int value = meizuFlags.getInt(lp);
                if (light) {
                    value |= bit;
                } else {
                    value &= ~bit;
                }

                meizuFlags.setInt(lp, value);
                window.setAttributes(lp);
                result = true;
            } catch (Exception var8) {
            }
        }

        return result;
    }

    public static boolean isFullScreen(Activity activity) {
        if (!supportTranslucent()) {
            return false;
        } else if (!QMUIDeviceHelper.isMeizu() && !QMUIDeviceHelper.isMIUI()) {
            if (QMUINotchHelper.isNotchOfficialSupport()) {
                return true;
            } else if (Build.VERSION.SDK_INT >= 21) {
                return Build.VERSION.SDK_INT >= 23 && supportTransclentStatusBar6() ? true : true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

    public static boolean isFullScreenWin(Activity activity) {
        boolean ret = false;

        try {
            WindowManager.LayoutParams attrs = activity.getWindow().getAttributes();
            ret = (attrs.flags & 1024) != 0;
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return ret;
    }

    public static Integer getStatusBarAPITransparentValue(Context context) {
        if (sTransparentValue != null) {
            return sTransparentValue;
        } else {
            String[] systemSharedLibraryNames = context.getPackageManager().getSystemSharedLibraryNames();
            String fieldName = null;
            String[] var3 = systemSharedLibraryNames;
            int var4 = systemSharedLibraryNames.length;

            for(int var5 = 0; var5 < var4; ++var5) {
                String lib = var3[var5];
                if ("touchwiz".equals(lib)) {
                    fieldName = "SYSTEM_UI_FLAG_TRANSPARENT_BACKGROUND";
                } else if (lib.startsWith("com.sonyericsson.navigationbar")) {
                    fieldName = "SYSTEM_UI_FLAG_TRANSPARENT";
                }
            }

            if (fieldName != null) {
                try {
                    Field field = View.class.getField(fieldName);
                    if (field != null) {
                        Class<?> type = field.getType();
                        if (type == Integer.TYPE) {
                            sTransparentValue = field.getInt((Object)null);
                        }
                    }
                } catch (Exception var7) {
                }
            }

            return sTransparentValue;
        }
    }

    public static boolean supportTransclentStatusBar6() {
        return !QMUIDeviceHelper.isZUKZ1() && !QMUIDeviceHelper.isZTKC2016();
    }

    public static int getStatusbarHeight(Context context) {
        if (sStatusbarHeight == -1) {
            initStatusBarHeight(context);
        }

        return sStatusbarHeight;
    }

    private static void initStatusBarHeight(Context context) {
        Object obj = null;
        Field field = null;

        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            obj = clazz.newInstance();
            if (QMUIDeviceHelper.isMeizu()) {
                try {
                    field = clazz.getField("status_bar_height_large");
                } catch (Throwable var6) {
                    var6.printStackTrace();
                }
            }

            if (field == null) {
                field = clazz.getField("status_bar_height");
            }
        } catch (Throwable var7) {
            var7.printStackTrace();
        }

        if (field != null && obj != null) {
            try {
                int id = Integer.parseInt(field.get(obj).toString());
                sStatusbarHeight = context.getResources().getDimensionPixelSize(id);
            } catch (Throwable var5) {
                var5.printStackTrace();
            }
        }

        if (QMUIDeviceHelper.isTablet(context) && sStatusbarHeight > QMUIDisplayHelper.dp2px(context, 25)) {
            sStatusbarHeight = 0;
        } else if (sStatusbarHeight <= 0) {
            if (sVirtualDensity == -1.0F) {
                sStatusbarHeight = QMUIDisplayHelper.dp2px(context, 25);
            } else {
                sStatusbarHeight = (int)(25.0F * sVirtualDensity + 0.5F);
            }
        }

    }

    public static void setVirtualDensity(float density) {
        sVirtualDensity = density;
    }

    public static void setVirtualDensityDpi(float densityDpi) {
        sVirtualDensityDpi = densityDpi;
    }
}
